#include "QTableWidgetOperate.h"
#include <QScrollBar>

QTableWidgetOperate::QTableWidgetOperate(int row, int column, QWidget *parent) : QTableWidget(row, column,parent), //传输同步传递到表格
    m_middleButtonPressed(false)
{
    setMouseTracking(true);

}

void QTableWidgetOperate::setScrollSpeed(double speed)
{
    scroll_speed = speed; //加载滑动的速度
}

void QTableWidgetOperate::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::MiddleButton) {
        m_middleButtonPressed = true;
        m_lastPos = event->pos();
        setCursor(Qt::ClosedHandCursor);
        event->accept();
        return;
    }
    QTableView::mousePressEvent(event);
}

void QTableWidgetOperate::mouseMoveEvent(QMouseEvent *event)
{
    if (m_middleButtonPressed) {
        QPoint delta = event->pos() - m_lastPos;
        horizontalScrollBar()->setValue(horizontalScrollBar()->value() - delta.x() * scroll_speed);
        verticalScrollBar()->setValue(verticalScrollBar()->value() - delta.y() * scroll_speed);
        m_lastPos = event->pos();
        event->accept();
        return;
    }
    QTableView::mouseMoveEvent(event);
}

void QTableWidgetOperate::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::MiddleButton) {
        m_middleButtonPressed = false;
        setCursor(Qt::ArrowCursor);
        event->accept();
        return;
    }
    QTableView::mouseReleaseEvent(event);
}    
